package com.renjibo.sharkerwave.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.adapter.HomeAdapter;
import com.renjibo.sharkerwave.adapter.HomeOneAdapter;
import com.renjibo.sharkerwave.adapter.HomeTwoAdapter;
import com.renjibo.sharkerwave.models.Course;
import com.renjibo.sharkerwave.models.data.CourseData;
import com.renjibo.sharkerwave.network.SharkerParams;
import com.renjibo.sharkerwave.ui.view.DividerItemDecoration;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 查看全部
 */
public class FreeAuditionFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private RecyclerView free_fragment_recyclerView;
    private List<Course> topicCourse=new ArrayList<>();

    public FreeAuditionFragment() {
    }


    public static FreeAuditionFragment newInstance(String param1, String param2) {
        FreeAuditionFragment fragment = new FreeAuditionFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_free_audition, container, false);
        TextView free_fragment_textView = (TextView) view.findViewById(R.id.free_fragment_textView);
        free_fragment_recyclerView = (RecyclerView) view.findViewById(R.id.free_fragment_recyclerView);
        free_fragment_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        free_fragment_recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        if (mParam1 != null && mParam1.equals("精选课程")) {
            //TODO
            listCourse("list_course", "", 20, 0, 1);
            //真实-
//            listCourse("list_course", mParam2, 20, 0, 1);

        } else {
            //TODO
            listTry("list_try","",20,0);
            //真实
//            listTry("list_try",mParam2,20,0);
        }


        free_fragment_textView.setText("egmwioengiwueng");
        return view;
    }

    /**
     * 试听课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     */
    void listTry(String uri, String category, int page_size, int page_index) {
        listCourse(uri, category, page_size, page_index, 0);
    }

    /**
     * 精品课程列表
     *
     * @param uri
     * @param category
     * @param page_index
     * @param sort_by
     */
    public void listCourse(final String uri, final String category, int page_size, int page_index, int sort_by) {
        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", page_size + "");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<CourseData>() {
            @Override
            public void onSuccess(CourseData result) {
//                Toast.makeText(getActivity(),"count:"+topicCourse.size()+"  "+mParam1,Toast.LENGTH_SHORT).show();
                    if(!TextUtils.isEmpty(mParam1)&&mParam1.equals("精选课程")){
//                        Toast.makeText(getActivity(),"newcount:"+topicCourse.size()+"  "+mParam1,Toast.LENGTH_SHORT).show();
//                        Log.d("zzz","分欧冠杯吴邦国i："+result.toString());
                        topicCourse.clear();
                        topicCourse.addAll(result.qualityCourse);
                        free_fragment_recyclerView.setAdapter(new HomeTwoAdapter(getActivity(),result.qualityCourse));
                    }else {
                        topicCourse.clear();
                        topicCourse.addAll(result.tryCourse);
                        free_fragment_recyclerView.setAdapter(new HomeOneAdapter(getActivity(),result.tryCourse));
                    }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
